#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10
#define M 5

int** getMemFor2DArray(int, int);
void randFilling(int**, int, int);
void printArr(int**, int, int);

int main(void)
{
    srand(time(NULL));

    int **pArr = getMemFor2DArray(M, N);

    if (NULL != pArr)
    {
        randFilling(pArr, M, N);
        printArr(pArr, M, N);

        for (int i = 0; i < M; i ++)
        {
            free(pArr[i]);
            pArr[i] = NULL;
        }

        free(pArr);
    }
    else
    {
        printf("Memory allocation error!\n");
    }

    return 0;
}

int** getMemFor2DArray(int m, int n)
{
    int **pMem = (int**)malloc(sizeof(int*) * m);

    for (int i = 0; i < m; i ++)
    {
        pMem[i] = (int*)malloc(sizeof(int) * n);
    }

    return pMem;
}

void randFilling(int **pMem, int m, int n)
{
    for (int i = 0; i < m; i ++)
    {
        for (int j = 0; j < n; j ++)
        {
            pMem[i][j] = rand() % 100;
        }
    }

}

void printArr(int **pMem, int m, int n)
{
    for (int i = 0; i < m; i ++)
    {
        for (int j = 0; j < n; j ++)
        {
            printf("%-3d ", pMem[i][j]);
        }
        printf("\n");
    }
}
